﻿#include "../../inc/helper.hpp"

const static char* g_msg_worker_ready = "\001";

int main()
{
	zmq::context_t ctx;
	zmq::socket_t worker(ctx, zmq::socket_type::req);

	std::string id = gen_random_id();
	worker.setsockopt(ZMQ_IDENTITY, id.data(), id.size());
	worker.connect("tcp://localhost:5556");

	info("{} worker ready", id);
	send_string(worker, g_msg_worker_ready);

	int cycles = 0;
	while (1)
	{
		auto msgs = zmsg_recv(worker);
		++cycles;

		if (cycles > 3 && rand() % 5 == 0)
		{
			info("simulating a crash");
			break;
		}
		else if (cycles > 3 && rand() % 5 == 0)
		{
			info("simulating CPU overload");
			std::this_thread::sleep_for(std::chrono::seconds(3));
		}

		info("{} normal reply", id);
		std::this_thread::sleep_for(std::chrono::seconds(1));
		zmsg_send(worker, msgs);
	}
}